package com.mdc.mansion.core.modules.basedata.impl;

import com.alibaba.fastjson.JSONObject;
import com.mdc.mansion.common.exception.BusinessException;
import com.mdc.mansion.core.common.translate.TranslateLanguage;
import com.mdc.mansion.core.common.translate.TranslateService;
import com.mdc.mansion.core.common.util.RestaurantSupport;
import com.mdc.mansion.core.modules.basedata.PushMessageService;
import com.mdc.mansion.dataaccess.common.context.ContextHolder;
import com.mdc.mansion.dataaccess.common.service.CrudService;
import com.mdc.mansion.dataaccess.modules.basedata.dao.PushMessageDao;
import com.mdc.mansion.dataaccess.modules.basedata.entity.PushMessage;
import com.mdc.mansion.dataaccess.modules.system.entity.User;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 消息推送ServiceImpl
 * @author mingdongc
 * @version 2018-01-21
 */
@Service
public class PushMessageServiceImpl extends CrudService<PushMessageDao, PushMessage> implements PushMessageService {

    @Resource
    private TranslateService translateService;
    @Override
    @Transactional
    public void pushNotifi( String message,int businessType) {
            PushMessage pushMessage=new PushMessage();
            User currentOperationUser = RestaurantSupport.getCurrentOperationUser();
            pushMessage.setUpdateBy(currentOperationUser);
            pushMessage.setCreateBy(currentOperationUser);
            pushMessage.setMessageType(PushMessage.MESSAGE_TYPE.NOTIFI.getVal());
            pushMessage.setMessageStatus(PushMessage.MESSAGE_STATUS.WAITE_SEND.getVal());
            String chinaMessage = translateService.translate(message, TranslateLanguage.CHINA);
            String englishMessage = translateService.translate(message, TranslateLanguage.ENGLISH);
            String indonesiaMessage = translateService.translate(message, TranslateLanguage.INDONESIA);
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("chinaMessage",chinaMessage);
            jsonObject.put("englishMessage",englishMessage);
            jsonObject.put("indonesiaMessage",indonesiaMessage);
            pushMessage.setMessage(jsonObject.toJSONString());
            pushMessage.setRestaurantId(ContextHolder.currentRestaurantId());
            pushMessage.setBusinessType(businessType);
            this.save(pushMessage);
    }

    @Override
    public void pushBusinessMessage(int type, String tableName, String dishesName) {
        if(type==PushMessage.BUSINESS_TYPE.USER_CALL.getVal() ||
                type==PushMessage.BUSINESS_TYPE.USER_ORDER.getVal() ||
                type==PushMessage.BUSINESS_TYPE.USER_URGE.getVal() ||
                type==PushMessage.BUSINESS_TYPE.SERVE_DISHES.getVal() ||
                type==PushMessage.BUSINESS_TYPE.USER_CHECKOUT.getVal() ){
            if(StringUtils.isBlank(tableName)){return;}
        }
        if(type==PushMessage.BUSINESS_TYPE.STOCKOUT.getVal() ||
                type==PushMessage.BUSINESS_TYPE.STOCKIN.getVal() ){
            if(StringUtils.isBlank(dishesName)){return;}
        }
        
        if(type==PushMessage.BUSINESS_TYPE.USER_CALL.getVal()){
            this.pushNotifi(tableName+"-用户呼叫",type);
        }else if(type==PushMessage.BUSINESS_TYPE.USER_ORDER.getVal()){
            this.pushNotifi(tableName+"-用户点菜",type);
        }else if(type==PushMessage.BUSINESS_TYPE.USER_URGE.getVal()){
            this.pushNotifi(tableName+"-用户催菜",type);
        }else if(type==PushMessage.BUSINESS_TYPE.SERVE_DISHES.getVal()){
            this.pushNotifi(tableName+"-上菜",type);
        }else if(type==PushMessage.BUSINESS_TYPE.STOCKOUT.getVal()){
            this.pushNotifi(dishesName+"-缺货",type);
        }else if(type==PushMessage.BUSINESS_TYPE.STOCKIN.getVal()){
            this.pushNotifi(dishesName+"-恢复供应",type);
        }else if(type==PushMessage.BUSINESS_TYPE.USER_CHECKOUT.getVal()){
            this.pushNotifi(tableName+"-用户结账",type);
        }
    }
}
